package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQueryObject;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository destinationRepository;

    @Autowired
    private IRegionService regionService;

    @Autowired
    private MongoTemplate template;

    @Override
    public void save(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public void delete(String id) {
        destinationRepository.deleteById(id);
    }

    @Override
    public void update(Destination destination) {
        destinationRepository.insert(destination);
    }

    @Override
    public Destination get(String id) {
        return destinationRepository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return destinationRepository.findAll();
    }

    @Override
    public Page<Destination> query(DestinationQueryObject qo) {
        Query query = new Query();

        // 高级查询
        if(StringUtils.hasLength(qo.getKeyword())) {
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        if(StringUtils.hasLength(qo.getParentId())) {
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        } else if (!StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("parentId").is(null));
        }


        // list
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        return  DBHelper.query(template, qo, query, pageable, Destination.class);
    }

    @Override
    public List<Destination> findByIdIn(List<String> refIds) {
        return destinationRepository.findByIdIn(refIds);
    }

    @Override
    public List<Destination> queryToast(String destId) {
        // 创建toast集合
        List<Destination> toasts = new ArrayList<>();
        // 获取当前目的地 id, 作为父id, 执行以下判断
        String parentId = destId;
        // 判断 parentId 是否为空
        while(StringUtils.hasLength(parentId)) {
            // 根据parentId, 获取父目的地（第一次是当前目的地）
            Destination destination = this.get(parentId);
            // 将父目的地添加到吐司集合中
            toasts.add(destination);
            // 继续获取parentId
            parentId = destination.getParentId();
        }
        // 将吐司进行反转 城市 >> 省份 >> 国家 ------ 国家 >> 省份 >> 城市
        Collections.reverse(toasts);
        // 返回吐司集合
        return toasts;
    }

    @Override
    public List<Destination> getDestByRegionId(String rid) {
        Region region = regionService.get(rid);
        List<String> refIds = region.getRefIds();
        return destinationRepository.findByIdIn(refIds);
    }

    @Override
    public List<Destination> getDestByRegionIdForApi(String rid) {
        // 创建一个空集合, 接收目的地
        List<Destination> destinations = new ArrayList<>();
        // 判断是否是 -1 --- 国内
        if("-1".equals(rid)) {
            destinations = destinationRepository.findByParentName("中国");
        } else {
            destinations = this.getDestByRegionId(rid);
        }
        // 通过destinations.parentId, 去把子目的地获取到
        for (Destination dest : destinations) {
            Pageable pageable = PageRequest.of(0, 6, Sort.Direction.ASC,"_id");
            List<Destination> childrens = destinationRepository.findByParentId(dest.getId(), pageable);
            dest.setChildren(childrens);
        }
        return destinations;
    }


    @Override
    public List<Destination> getDestByDeep(int deep) {
        return destinationRepository.findByDeep(deep);
    }

    @Override
    public void updateInfo(String id, String info) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.set("info", info);
        template.updateFirst(query, update, Destination.class);
    }
}
